#include<iostream>
using namespace std;
int main()
{
	int s, q;
	cin >> s >> q;
	while (q--) {
		int x;
		cin >> x;
		if (x % 3 == 0) {
			cout << 0 << endl;
			continue;
		}
		cout << s - x % s << endl;
	}
	return 0;
}